package tongcheng.com.tongcheng.ui.yemian.shenfen.activity.install;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.yanzhenjie.nohttp.RequestMethod;

import butterknife.Bind;
import butterknife.ButterKnife;
import tongcheng.com.tongcheng.ConStans;
import tongcheng.com.tongcheng.R;
import tongcheng.com.tongcheng.base.BaseActivity;
import tongcheng.com.tongcheng.bean.PeoPleDetialBean;
import tongcheng.com.tongcheng.bean.YueDetialBean;
import tongcheng.com.tongcheng.network.CallServer;
import tongcheng.com.tongcheng.network.MyRequest;
import tongcheng.com.tongcheng.network.OnCallBack;
import tongcheng.com.tongcheng.utils.GlideApp;
import tongcheng.com.tongcheng.utils.GsonUtil;
import tongcheng.com.tongcheng.utils.Setting;
import tongcheng.com.tongcheng.utils.ShowToast;
import tongcheng.com.tongcheng.widght.MyRatingBar;

public class InstallPeopleDetialActivity extends BaseActivity {

    @Bind(R.id.back)
    ImageView mBack;
    @Bind(R.id.back_text)
    TextView mBackText;
    @Bind(R.id.title)
    TextView mTitle;
    @Bind(R.id.title_bar)
    RelativeLayout mBg;
    @Bind(R.id.installdetial_ll1)
    LinearLayout mInstalldetialLl1;
    @Bind(R.id.installdetial_ll2)
    LinearLayout mInstalldetialLl2;
    @Bind(R.id.installdetial_ll3)
    LinearLayout mInstalldetialLl3;
    @Bind(R.id.installdetial_jieyue)
    Button mInstalldetialJieyue;
    @Bind(R.id.title_line)
    View mTitleLine;
    @Bind(R.id.installdetial_fen)
    Button mInstalldetialFen;
    @Bind(R.id.installdetial_beizhu)
    TextView mInstalldetialBeizhu;
    @Bind(R.id.installdetial_shenfenzheng)
    TextView mInstalldetialShenfenzheng;
    @Bind(R.id.installdetial_ispeixun)
    TextView mInstalldetialIspeixun;
    @Bind(R.id.installdetial_zhengshu)
    TextView mInstalldetialZhengshu;
    @Bind(R.id.installdetial_icon1)
    ImageView mInstalldetialIcon1;
    @Bind(R.id.installdetial_icon2)
    ImageView mInstalldetialIcon2;
    @Bind(R.id.installdetial_icon3)
    ImageView mInstalldetialIcon3;
    @Bind(R.id.installdetial_rating1)
    MyRatingBar mInstalldetialRating1;
    @Bind(R.id.installdetial_rating2)
    MyRatingBar mInstalldetialRating2;
    @Bind(R.id.installdetial_task)
    TextView mInstalldetialTask;
    @Bind(R.id.installdetial_touxiang)
    ImageView mInstalldetialTouxiang;
    @Bind(R.id.installdetial_name)
    TextView mInstalldetialName;
    private int mId;
    private String mIs_qIanyue;


    @Override
    public Integer initResid() {
        return R.layout.activity_install_people_detial;
    }

    @Override
    public void initCreate(Bundle savedInstanceState) {


        settitlebar(mBg);
        mBg.setBackgroundColor(Color.TRANSPARENT);
        mBackText.setText("返回");
        mBackText.setVisibility(View.VISIBLE);
        mBackText.setTextColor(Color.BLACK);
     /*   mBack.setImageResource(R.mipmap.fanhui_geren);*/
        mTitleLine.setVisibility(View.GONE);
        mTitle.setText("");
        mId = getIntent().getIntExtra("id", -1);
        mIs_qIanyue = getIntent().getStringExtra("qianyue");
        if (mIs_qIanyue != null) {
            switch (mIs_qIanyue) {
                case "1"://1签约等待确认
                    mInstalldetialJieyue.setText("等待签约确认");
                    break;
                case "2"://2完成签约
                    mInstalldetialJieyue.setText("解约");
                    break;
                case "3"://3未签约
                    mInstalldetialJieyue.setText("签约");
                    break;
                case "4"://4解约待确认
                    mInstalldetialJieyue.setText("等待解约确认");
                    break;
                case "-1"://没状态.默认-1,签约
                    mInstalldetialJieyue.setText("签约");
                    break;

            }
        }




        mBackText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


        mInstalldetialJieyue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String trim = mInstalldetialJieyue.getText().toString().trim();
                if (trim.equals("解约") || trim.equals("签约")) {
                    Intent intent = new Intent(InstallPeopleDetialActivity.this, JieYueActivity.class);
                    intent.putExtra("qianyue", mIs_qIanyue);
                    intent.putExtra("id", mId);
                    startActivity(intent);
                } else {
                    ShowToast.showToast("确认中,请耐心等待");
                }
            }
        });




        //初始化数据
        initData();
    }

    private void initData() {
        MyRequest request = new MyRequest(ConStans.InfoDetial_Url);
        request.add("id", mId);
        CallServer.getInstance().addPost(this, request, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
                PeoPleDetialBean peoPleDetialBean = GsonUtil.GsonToBean(json, PeoPleDetialBean.class);
                if (peoPleDetialBean != null) {
                    PeoPleDetialBean.DataBean data = peoPleDetialBean.data;
                    GlideApp.with(InstallPeopleDetialActivity.this).load(ConStans.BaseUrl_Test_url + data.avatar).placeholder(R.mipmap.touxiang)
                            .error(R.mipmap.touxiang).transform(new CircleCrop()).into(mInstalldetialTouxiang);//加载头像


                    switch (data.is_sign) {
                        case 1://1签约等待确认
                            mInstalldetialJieyue.setText("等待签约确认");
                            break;
                        case 2://2完成签约
                            mInstalldetialJieyue.setText("解约");
                            break;
                        case 3://3未签约
                            mInstalldetialJieyue.setText("签约");
                            break;
                        case 4://4解约待确认
                            mInstalldetialJieyue.setText("等待解约确认");
                            break;
                        case -1://没状态.默认-1,签约
                            mInstalldetialJieyue.setText("签约");
                            break;

                    }


                    mInstalldetialName.setText(data.user_name);//设置名字
                    mInstalldetialFen.setText("信用积分:" + data.credit_score + ">");//信用分
                    mInstalldetialBeizhu.setText(data.record + "");//备注
                    mInstalldetialShenfenzheng.setText(data.idno + "");//身份证号码
                    if (data.is_train != null) {//是否培训
                        if (data.is_train.equals("1")) {
                            mInstalldetialIspeixun.setText("是");
                        } else {
                            mInstalldetialIspeixun.setText("否");
                        }
                    } else {
                        mInstalldetialIspeixun.setText("否");
                    }


                    String documents_features = data.documents_features;
                    String[] split = documents_features.split(",");
                    for (int i = 0; i < split.length; i++) {
                        String s = split[i];
                        String url = ConStans.BaseUrl_Test_url + s;
                        if (i == 0) {
                            GlideApp.with(InstallPeopleDetialActivity.this).load(url).error(R.mipmap.moren).into(mInstalldetialIcon1);
                        } else if (i == 1) {
                            GlideApp.with(InstallPeopleDetialActivity.this).load(url).error(R.mipmap.moren).into(mInstalldetialIcon2);
                        } else if (i == 2) {
                            GlideApp.with(InstallPeopleDetialActivity.this).load(url).error(R.mipmap.moren).into(mInstalldetialIcon3);
                        }
                    }


                    mInstalldetialRating1.setClickable(false);
                    if (!TextUtils.isEmpty(data.install_score)) {
                        float v = Float.parseFloat(data.install_score);
                        mInstalldetialRating1.setStar(v);
                    }


                    mInstalldetialRating2.setClickable(false);
                    if (!TextUtils.isEmpty(data.overall_score)) {
                        float v = Float.parseFloat(data.overall_score);
                        mInstalldetialRating2.setStar(v);
                    }


                    mInstalldetialTask.setText(data.now_task+"");

                }
            }
        });
    }


    private void initYueState() {
        MyRequest request = new MyRequest(ConStans.YueDetial_Url, RequestMethod.POST);
        request.add("usreid", Setting.getId());
        request.add("searchid", mId);
        CallServer.getInstance().addPost(this, request, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
               /* MyRequest request = new MyRequest(ConStans.Jieyue_Url, RequestMethod.POST);*/
                YueDetialBean yueDetialBean = GsonUtil.GsonToBean(json, YueDetialBean.class);
                if (yueDetialBean != null) {
                    if (yueDetialBean.data != null) {
                        YueDetialBean.DataBean data = yueDetialBean.data;

                        switch (data.is_sign) {
                            case 1://1签约等待确认
                                mInstalldetialJieyue.setText("等待签约确认");
                                break;
                            case 2://2完成签约
                                mInstalldetialJieyue.setText("解约");
                                break;
                            case 3://3未签约
                                mInstalldetialJieyue.setText("签约");
                                break;
                            case 4://4解约待确认
                                mInstalldetialJieyue.setText("等待解约确认");
                                break;
                            case -1://没状态.默认-1,签约
                                mInstalldetialJieyue.setText("签约");
                                break;
                        }

                    }
                }

            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    @Override
    protected void onResume() {
        super.onResume();
        //初始化数据
        initYueState();
       // initData();
    }
}
